package com.xzlf.service;

import com.xzlf.config.Constans;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Order(2)
@Slf4j
public class ListenerQueueSizeUP implements CommandLineRunner {

    @Autowired
    private DemoService demoService;

    @Override
    public void run(String... args) throws Exception {
        new Thread(() ->{
            try {
                TimeUnit.SECONDS.sleep(10);
                String name = Thread.currentThread().getName();
                log.info("==========开启同步线程监听=========");
                Constans.REENTRANT_LOCK_UP.lock();
                while (true){
                    while (demoService.getBuffersize() == 0){
                        Constans.ASYNCUP.signal();
                        Constans.LISTENEUP.await();
                    }
                    log.info("========{} 监听到{} 个同步线程正则运行===========",name, demoService.getBuffersize());
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                Constans.REENTRANT_LOCK.unlock();
            }
        }).start();

    }
}
